import Dao.BookDao;
import Dao.impl.BookDaoImpl;
import Service.BookService;
import Service.impl.BookServiceImpl;
import com.google.errorprone.annotations.Var;
import db.DB;
import fake.domain.Book.Book;
import utils.RandomUtil;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Scanner;

/**
 * @ClassName:App
 * @Author: 鹿建文
 * @Date: 2022/3/24  - 13:38
 * @Describe:
 */
public class App {
    public static void main(String[] args) throws IOException {
//        DB db = new DB();
        BookDao bookDao = new BookDaoImpl();
        BookService bookService = new BookServiceImpl();
        Scanner scanner = new Scanner(System.in);
        int op = -1;
        System.out.println("1、查看所有的图书，并将其保存到指定文件中");
        System.out.println("2、查找图书");
        System.out.println("3、增加图书");
        System.out.println("4、修改图书");
        System.out.println("-1、退出");
        System.out.println("请选择");
        op = scanner.nextInt();
        while (op != -1) {
            switch (op) {
                case 1:
                    System.out.println("正在生成.....");
                    App.writein();
                    DB.books.forEach(System.out::println);
                    break;
                case 2:
                    System.out.println("1、根据isbn查找图书");
                    System.out.println("2、根据书名查找图书");
                    System.out.println("3、根据作者查找图书");
                    System.out.println("4、根据价格区间查找图书");
                    System.out.println("5、根据出版社查找图书");
                    System.out.println("-1、退出");
                    System.out.println("请选择");
                    op = scanner.nextInt();
                    switch (op) {
                        case 1:
                            System.out.println("请输入ISBN");
                            String isbn = scanner.next();
                            Book isbnres = bookService.getBookByISBN(isbn);
                            System.out.println(isbnres);
                            break;
                        case 2:
                            System.out.println("请输入书名");
                            String bname = scanner.next();
                            System.out.println(bookService.getBookBybname(bname));
                            break;

                        case 3:
                            System.out.println("请输入作者");
                            String author = scanner.next();
                            System.out.println(bookService.getBookByauthor(author));

                            break;
                        case 4:
                            System.out.println("请输入价格区间");

                            break;
                            case 5:
                            System.out.println("请输入出版社");
                                String press = scanner.next();
                                System.out.println(bookService.getBookByauthor(press));
                            break;

                        case -1:
                            System.out.println("退出查找");
                            break;
                        default:
                            System.out.println("输入信息有误");
                            break;


                    }
                    break;


                case 3:
                    System.out.println("增加图书");
                    Book addbook = addbook(scanner);
                    System.out.println(bookService.addbook(addbook)?"增加陈工":"增加失败");
                    DB.books.forEach(System.out::println);


                    break;
                case 4:
                    System.out.println("修改图书");
                    Book bookxiugai =addbook(scanner) ;
                    System.out.println(bookService.updatebook(bookxiugai)?"修改成功":"修改失败");
                    DB.books.forEach(System.out::println);


                    break;

                default:
                    break;

            }
            System.out.println("1、查看所有的图书，并将其保存到指定文件中");
            System.out.println("2、查找图书");
            System.out.println("3、增加图书");
            System.out.println("4、修改图书");
            System.out.println("-1、退出");
            System.out.println("请选择");
            op = scanner.nextInt();


        }
        scanner.close();
        if (scanner != null) {
            scanner.close();
        }



    }

    private static void writein () throws IOException {
        File file = new File("src/main/java/books500");
        if (!(file.exists())) {
            boolean newFile = file.createNewFile();
        }
        BufferedWriter bw = new BufferedWriter(new FileWriter(file));
        for (Book book : DB.books) {
            bw.write("\t" + book.getIsbn() + "\t" + book.getBname() + "\t" + book.getAuthor() + "\t" + book.getDate() + "\t" + book.getPress() + "\t" + book.getPrice() + "\n");
        }
        bw.flush();
        ;
        if (bw != null) {
            bw.close();
        }
        System.out.println("请到根目录下查看");

    }

    private static Book addbook(Scanner scanner){
        System.out.println("请输入ISBN");
        String ISBN = scanner.next();
        System.out.println("请输入书名");
        String bname = scanner.next();
        System.out.println("请输入作者");
        String author = scanner.next();
        System.out.println("请输入出版社");
        String press = scanner.next();
        System.out.println("请输入书价");
        BigDecimal price = scanner.nextBigDecimal();
        Book book = new Book(ISBN, bname, author, RandomUtil.genLocalDate("2000-1-1", "2022-3-24"), press, price);
        return book;


    }
}
